package com.wangmeng.book.updater;

import com.wangmeng.book.dao.BaseDaoSupport;
import com.wangmeng.book.entity.*;
import com.wangmeng.book.utils.ConstValueUtils;
import com.wangmeng.book.utils.SpringContextManager;
import com.wangmeng.book.utils.StringUtils;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.CoreConnectionPNames;
import org.apache.log4j.Logger;
import org.cyberneko.html.parsers.DOMParser;
import org.hibernate.engine.jdbc.BlobProxy;
import org.json.JSONObject;
import org.springframework.jdbc.core.JdbcTemplate;
import org.w3c.dom.*;
import org.xml.sax.InputSource;
import sun.misc.BASE64Decoder;

import java.io.ByteArrayOutputStream;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * User: 王猛
 * Date: 13-9-13
 */
public class BookFromWy implements IBookUpdater {
    private static final Logger logger = Logger.getLogger(BookFromWy.class);
    private static final String CHARSET_HTML = "utf-8";
    private static final String URL_INDEX = "http://yuedu.163.com/book/category/category/1_1_0_1/p#{page}/s20";
    private static final String URL_YC_MM = "http://yuedu.163.com/ycmm/category/1_0_0_0_2/p#{page}/s30";
    private static final String URL_YC = "http://yuedu.163.com/yc/category/1_0_0_0_2/p#{page}/s30";
    private static final String URL_FIX = "http://yuedu.163.com";

    public void updateList() {
        //更新图书
        Document doc = getDocument(URL_INDEX.replace("#{page}", "1"));
        if (doc != null) {
            NodeList divList = doc.getElementsByTagName("DIV");
            BaseDaoSupport daoSupport = SpringContextManager.getContext().getBean(BaseDaoSupport.class);
            String checkSql = " and name = ? and author = ? ";
            int endPage = 1;
            for (int i = 0; i < divList.getLength(); i++) {
                Node divNode = divList.item(i);
                if (divNode.hasAttributes()) {
                    NamedNodeMap attrMap = divNode.getAttributes();
                    Node clsAttrNode = attrMap.getNamedItem("class");
                    if (clsAttrNode != null) {
                        if (clsAttrNode.getNodeValue().contains("yd-book-content")) {
                            //书的项目
                            NodeList aList = ((Element) divNode).getElementsByTagName("A");
                            for (int a = 0; a < aList.getLength(); a++) {
                                Node aNode = aList.item(a);
                                String bookName = aNode.getTextContent().trim();
                                String bookHref = aNode.getAttributes().getNamedItem("href").getNodeValue().trim();
                                NodeList imgList = ((Element) aNode).getElementsByTagName("IMG");
                                String imgHref = null;
                                if (imgList.getLength() > 0) {
                                    imgHref = imgList.item(0).getAttributes().getNamedItem("src").getNodeValue().trim();
                                }
                                String author = "";
                                NodeList dList = ((Element) aNode.getParentNode()).getElementsByTagName("DD");
                                if (dList.getLength() > 0) {
                                    author = dList.item(0).getTextContent().trim();
                                    if (!StringUtils.isTrimEmpty(author)) {
                                        List<BookDoc> bdList = daoSupport.listEntityByCondition(BookDoc.class, checkSql, new Object[]{bookName, author});
                                        if (bdList.size() == 0) {
                                            BookDoc bd = new BookDoc();
                                            bd.setName(bookName);
                                            bd.setAuthor(author);
                                            bd.setSourceId(bookHref);
                                            bd.setPublisher("");
                                            bd.setStatus(ConstValueUtils.STATUS_FINISHED);
                                            bd.setType(ConstValueUtils.BOOK_TYPE_COMMON);
                                            bd.setSourceType(ConstValueUtils.SOURCE_TYPE_WY);
                                            bd.setFree(1);
                                            bd.setHref(URL_FIX + bookHref);
                                            bd.setHasIcon(false);
                                            bd.setApproveStatus(ConstValueUtils.APPROVE_INIT);
                                            if (imgHref != null) {
                                                byte[] data = downHttpData(imgHref);
                                                if (data.length > 0) {
                                                    bd.setHasIcon(true);
                                                    bd.setIcon(BlobProxy.generateProxy(data));
                                                }
                                            }
                                            dirDetail(bd);
                                        }
                                    }
                                }
                            }
                        } else if (clsAttrNode.getNodeValue().contains("m-page")) {
                            //查询所有页
                            NodeList aList = ((Element) divNode).getElementsByTagName("A");
                            if (aList.getLength() > 0) {
                                Node endNode = aList.item(aList.getLength() - 2);
                                try {
                                    endPage = Integer.valueOf(endNode.getTextContent().trim());
                                } catch (Exception ex) {

                                }
                            }
                        }
                    }
                }
            }
            //处理余下的页
            for (int p = 2; p < endPage; p++) {
                doc = getDocument(URL_INDEX.replace("#{page}", String.valueOf(p)));
                if (doc != null) {
                    divList = doc.getElementsByTagName("DIV");
                    for (int i = 0; i < divList.getLength(); i++) {
                        Node divNode = divList.item(i);
                        if (divNode.hasAttributes()) {
                            NamedNodeMap attrMap = divNode.getAttributes();
                            Node clsAttrNode = attrMap.getNamedItem("class");
                            if (clsAttrNode != null) {
                                if (clsAttrNode.getNodeValue().contains("yd-book-content")) {
                                    //书的项目
                                    NodeList aList = ((Element) divNode).getElementsByTagName("A");
                                    for (int a = 0; a < aList.getLength(); a++) {
                                        Node aNode = aList.item(a);
                                        String bookName = aNode.getTextContent().trim();
                                        String bookHref = aNode.getAttributes().getNamedItem("href").getNodeValue().trim();
                                        NodeList imgList = ((Element) aNode).getElementsByTagName("IMG");
                                        String imgHref = null;
                                        if (imgList.getLength() > 0) {
                                            imgHref = imgList.item(0).getAttributes().getNamedItem("src").getNodeValue().trim();
                                        }
                                        String author = "";
                                        NodeList dList = ((Element) aNode.getParentNode()).getElementsByTagName("DD");
                                        if (dList.getLength() > 0) {
                                            author = dList.item(0).getTextContent().trim();
                                            if (!StringUtils.isTrimEmpty(author)) {
                                                List<BookDoc> bdList = daoSupport.listEntityByCondition(BookDoc.class, checkSql, new Object[]{bookName, author});
                                                if (bdList.size() == 0) {
                                                    BookDoc bd = new BookDoc();
                                                    bd.setName(bookName);
                                                    bd.setAuthor(author);
                                                    bd.setSourceId(bookHref);
                                                    bd.setPublisher("");
                                                    bd.setStatus(ConstValueUtils.STATUS_FINISHED);
                                                    bd.setType(ConstValueUtils.BOOK_TYPE_COMMON);
                                                    bd.setSourceType(ConstValueUtils.SOURCE_TYPE_WY);
                                                    bd.setFree(1);
                                                    bd.setHref(URL_FIX + bookHref);
                                                    bd.setApproveStatus(ConstValueUtils.APPROVE_INIT);
                                                    bd.setHasIcon(false);
                                                    if (imgHref != null) {
                                                        byte[] data = downHttpData(imgHref);
                                                        if (data.length > 0) {
                                                            bd.setHasIcon(true);
                                                            bd.setIcon(BlobProxy.generateProxy(data));
                                                        }
                                                    }
                                                    dirDetail(bd);
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        //更新原创男频
        doc = getDocument(URL_YC.replace("#{page}", "1"));
        if (doc != null) {
            NodeList tableList = doc.getElementsByTagName("TABLE");
            BaseDaoSupport daoSupport = SpringContextManager.getContext().getBean(BaseDaoSupport.class);
            String checkSql = " and name = ? and author = ? ";
            int endPage = 1;
            for (int i = 0; i < tableList.getLength(); i++) {
                Node tableNode = tableList.item(i);
                if (tableNode.hasAttributes()) {
                    NamedNodeMap attrMap = tableNode.getAttributes();
                    Node clsAttrNode = attrMap.getNamedItem("class");
                    if (clsAttrNode != null) {
                        if (clsAttrNode.getNodeValue().contains("yd-table")) {
                            //书的项目
                            NodeList trList = ((Element) tableNode).getElementsByTagName("TR");
                            for (int a = 0; a < trList.getLength(); a++) {
                                Node trNode = trList.item(a);
                                String cls = trNode.getAttributes().getNamedItem("class").getNodeValue().trim();
                                if (cls.contains("odd") || cls.contains("even")) {
                                    NodeList tdList = ((Element) trNode).getElementsByTagName("TD");
                                    NodeList hrefNode = ((Element) tdList.item(1)).getElementsByTagName("A");
                                    String bookName = hrefNode.item(0).getTextContent().trim();
                                    String bookHref = hrefNode.item(0).getAttributes().getNamedItem("href").getNodeValue().trim();
                                    String author = tdList.item(2).getTextContent().trim();
                                    Integer wordNum = Integer.valueOf(tdList.item(3).getTextContent().trim());
                                    List<BookDoc> bdList = daoSupport.listEntityByCondition(BookDoc.class, checkSql, new Object[]{bookName, author});
                                    if (bdList.size() == 0) {
                                        BookDoc bd = new BookDoc();
                                        bd.setName(bookName);
                                        bd.setAuthor(author);
                                        bd.setSourceId(bookHref);
                                        bd.setPublisher("");
                                        bd.setType(ConstValueUtils.BOOK_TYPE_COMMON);
                                        bd.setSourceType(ConstValueUtils.SOURCE_TYPE_WY);
                                        bd.setFree(1);
                                        bd.setWordNum(wordNum);
                                        bd.setHref(URL_FIX + bookHref);
                                        bd.setApproveStatus(ConstValueUtils.APPROVE_INIT);
                                        bd.setHasIcon(false);
                                        dirDetail2(bd);
                                    }
                                }
                            }
                        }
                    }
                }
            }
            NodeList divList = doc.getElementsByTagName("DIV");
            for (int d = 0; d < divList.getLength(); d++) {
                Node divNode = divList.item(d);
                if (divNode.hasAttributes()) {
                    Node clsNode = divNode.getAttributes().getNamedItem("class");
                    if (clsNode != null && clsNode.getNodeValue().contains("m-page")) {
                        NodeList aList = ((Element) divNode).getElementsByTagName("A");
                        if (aList.getLength() > 0) {
                            Node endNode = aList.item(aList.getLength() - 2);
                            try {
                                endPage = Integer.valueOf(endNode.getTextContent().trim());
                            } catch (Exception ex) {

                            }
                        }
                        break;
                    }
                }
            }
            //处理余下的页
            for (int p = 2; p < endPage; p++) {
                doc = getDocument(URL_YC.replace("#{page}", p + ""));
                if (doc != null) {
                    tableList = doc.getElementsByTagName("TABLE");
                    for (int i = 0; i < tableList.getLength(); i++) {
                        Node tableNode = tableList.item(i);
                        if (tableNode.hasAttributes()) {
                            NamedNodeMap attrMap = tableNode.getAttributes();
                            Node clsAttrNode = attrMap.getNamedItem("class");
                            if (clsAttrNode != null) {
                                if (clsAttrNode.getNodeValue().contains("yd-table")) {
                                    //书的项目
                                    NodeList trList = ((Element) tableNode).getElementsByTagName("TR");
                                    for (int a = 0; a < trList.getLength(); a++) {
                                        Node trNode = trList.item(a);
                                        String cls = trNode.getAttributes().getNamedItem("class").getNodeValue().trim();
                                        if (cls.contains("odd") || cls.contains("even")) {
                                            NodeList tdList = ((Element) trNode).getElementsByTagName("TD");
                                            NodeList hrefNode = ((Element) tdList.item(1)).getElementsByTagName("A");
                                            String bookName = hrefNode.item(0).getTextContent().trim();
                                            String bookHref = hrefNode.item(0).getAttributes().getNamedItem("href").getNodeValue().trim();
                                            String author = tdList.item(2).getTextContent().trim();
                                            Integer wordNum = Integer.valueOf(tdList.item(3).getTextContent().trim());
                                            List<BookDoc> bdList = daoSupport.listEntityByCondition(BookDoc.class, checkSql, new Object[]{bookName, author});
                                            if (bdList.size() == 0) {
                                                BookDoc bd = new BookDoc();
                                                bd.setName(bookName);
                                                bd.setAuthor(author);
                                                bd.setSourceId(bookHref);
                                                bd.setPublisher("");
                                                bd.setApproveStatus(ConstValueUtils.APPROVE_INIT);
                                                bd.setType(ConstValueUtils.BOOK_TYPE_COMMON);
                                                bd.setSourceType(ConstValueUtils.SOURCE_TYPE_WY);
                                                bd.setFree(1);
                                                bd.setWordNum(wordNum);
                                                bd.setHref(URL_FIX + bookHref);
                                                bd.setHasIcon(false);
                                                dirDetail2(bd);
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }

        //更新原创女频
        doc = getDocument(URL_YC_MM.replace("#{page}", "1"));
        if (doc != null) {
            NodeList tableList = doc.getElementsByTagName("TABLE");
            BaseDaoSupport daoSupport = SpringContextManager.getContext().getBean(BaseDaoSupport.class);
            String checkSql = " and name = ? and author = ? ";
            int endPage = 1;
            for (int i = 0; i < tableList.getLength(); i++) {
                Node tableNode = tableList.item(i);
                if (tableNode.hasAttributes()) {
                    NamedNodeMap attrMap = tableNode.getAttributes();
                    Node clsAttrNode = attrMap.getNamedItem("class");
                    if (clsAttrNode != null) {
                        if (clsAttrNode.getNodeValue().contains("yd-table")) {
                            //书的项目
                            NodeList trList = ((Element) tableNode).getElementsByTagName("TR");
                            for (int a = 0; a < trList.getLength(); a++) {
                                Node trNode = trList.item(a);
                                String cls = trNode.getAttributes().getNamedItem("class").getNodeValue().trim();
                                if (cls.contains("odd") || cls.contains("even")) {
                                    NodeList tdList = ((Element) trNode).getElementsByTagName("TD");
                                    NodeList hrefNode = ((Element) tdList.item(1)).getElementsByTagName("A");
                                    String bookName = hrefNode.item(0).getTextContent().trim();
                                    String bookHref = hrefNode.item(0).getAttributes().getNamedItem("href").getNodeValue().trim();
                                    String author = tdList.item(2).getTextContent().trim();
                                    Integer wordNum = Integer.valueOf(tdList.item(3).getTextContent().trim());
                                    List<BookDoc> bdList = daoSupport.listEntityByCondition(BookDoc.class, checkSql, new Object[]{bookName, author});
                                    if (bdList.size() == 0) {
                                        BookDoc bd = new BookDoc();
                                        bd.setName(bookName);
                                        bd.setAuthor(author);
                                        bd.setSourceId(bookHref);
                                        bd.setPublisher("");
                                        bd.setApproveStatus(ConstValueUtils.APPROVE_INIT);
                                        bd.setType(ConstValueUtils.BOOK_TYPE_COMMON);
                                        bd.setSourceType(ConstValueUtils.SOURCE_TYPE_WY);
                                        bd.setFree(1);
                                        bd.setWordNum(wordNum);
                                        bd.setHref(URL_FIX + bookHref);
                                        bd.setHasIcon(false);
                                        dirDetail2(bd);
                                    }
                                }
                            }
                        }
                    }
                }
            }
            NodeList divList = doc.getElementsByTagName("DIV");
            for (int d = 0; d < divList.getLength(); d++) {
                Node divNode = divList.item(d);
                if (divNode.hasAttributes()) {
                    Node clsNode = divNode.getAttributes().getNamedItem("class");
                    if (clsNode != null && clsNode.getNodeValue().contains("m-page")) {
                        NodeList aList = ((Element) divNode).getElementsByTagName("A");
                        if (aList.getLength() > 0) {
                            Node endNode = aList.item(aList.getLength() - 2);
                            try {
                                endPage = Integer.valueOf(endNode.getTextContent().trim());
                            } catch (Exception ex) {

                            }
                        }
                        break;
                    }
                }
            }
            //处理余下的页
            for (int p = 2; p < endPage; p++) {
                doc = getDocument(URL_YC_MM.replace("#{page}", p+""));
                if (doc != null) {
                    tableList = doc.getElementsByTagName("TABLE");
                    for (int i = 0; i < tableList.getLength(); i++) {
                        Node tableNode = tableList.item(i);
                        if (tableNode.hasAttributes()) {
                            NamedNodeMap attrMap = tableNode.getAttributes();
                            Node clsAttrNode = attrMap.getNamedItem("class");
                            if (clsAttrNode != null) {
                                if (clsAttrNode.getNodeValue().contains("yd-table")) {
                                    //书的项目
                                    NodeList trList = ((Element) tableNode).getElementsByTagName("TR");
                                    for (int a = 0; a < trList.getLength(); a++) {
                                        Node trNode = trList.item(a);
                                        String cls = trNode.getAttributes().getNamedItem("class").getNodeValue().trim();
                                        if (cls.contains("odd") || cls.contains("even")) {
                                            NodeList tdList = ((Element) trNode).getElementsByTagName("TD");
                                            NodeList hrefNode = ((Element) tdList.item(1)).getElementsByTagName("A");
                                            String bookName = hrefNode.item(0).getTextContent().trim();
                                            String bookHref = hrefNode.item(0).getAttributes().getNamedItem("href").getNodeValue().trim();
                                            String author = tdList.item(2).getTextContent().trim();
                                            Integer wordNum = Integer.valueOf(tdList.item(3).getTextContent().trim());
                                            List<BookDoc> bdList = daoSupport.listEntityByCondition(BookDoc.class, checkSql, new Object[]{bookName, author});
                                            if (bdList.size() == 0) {
                                                BookDoc bd = new BookDoc();
                                                bd.setName(bookName);
                                                bd.setAuthor(author);
                                                bd.setSourceId(bookHref);
                                                bd.setPublisher("");
                                                bd.setApproveStatus(ConstValueUtils.APPROVE_INIT);
                                                bd.setType(ConstValueUtils.BOOK_TYPE_COMMON);
                                                bd.setSourceType(ConstValueUtils.SOURCE_TYPE_WY);
                                                bd.setFree(1);
                                                bd.setWordNum(wordNum);
                                                bd.setHref(URL_FIX + bookHref);
                                                bd.setHasIcon(false);
                                                dirDetail2(bd);
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }

    public int loadContent(BookDoc bd) {
        try {
            BaseDaoSupport daoSupport = SpringContextManager.getContext().getBean(BaseDaoSupport.class);
            Document doc = getDocument(URL_FIX + bd.getSourceId());
            if (doc != null) {
                BASE64Decoder decoder = new BASE64Decoder();
                String startFlag = "<div class=\"m-content\">";
                String endFlag = "<div id=\"book-bottom\">";
                NodeList emtList = doc.getElementsByTagName("DIV");
                int sNum = 0;
                int pOrder = 0;
                for (int i = 0; i < emtList.getLength(); i++) {
                    Node divNode = emtList.item(i);
                    if (divNode.hasAttributes()) {
                        NamedNodeMap attrMap = divNode.getAttributes();
                        Node clsAttrNode = attrMap.getNamedItem("class");
                        if (clsAttrNode != null) {
                            if (clsAttrNode.getNodeValue().contains("m-volume")) {
                                sNum++;
                                //处理章节
                                NodeList tList = ((Element) divNode).getElementsByTagName("H4");
                                String sectionName = "第" + sNum + "部分";
                                if (tList.getLength() > 0) {
                                    sectionName = tList.item(0).getTextContent().trim();
                                }
                                BookDocSection bds = new BookDocSection();
                                bds.setBookId(bd.getId());
                                bds.setName(sectionName);
                                bds.setOrder(sNum);
                                daoSupport.saveEntity(bds);
                                NodeList ulList = ((Element) divNode).getElementsByTagName("UL");
                                if (ulList.getLength() > 0) {
                                    NodeList aList = ((Element) ulList.item(0)).getElementsByTagName("A");
                                    for (int a = 0; a < aList.getLength(); a++) {
                                        pOrder++;
                                        Node aNode = aList.item(a);
                                        String title = aNode.getTextContent().trim();
                                        String pageHref = aNode.getAttributes().getNamedItem("href").getNodeValue().trim();
                                        String[] hvs = pageHref.split("/");
                                        if (hvs.length > 2) {
                                            HttpClient hc = new DefaultHttpClient();
                                            hc.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 60000);
                                            hc.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 30000);
                                            HttpGet contentGet = new HttpGet("http://yuedu.163.com/getArticleContent.do?sourceUuid=" + hvs[hvs.length - 2] + "&articleUuid=" + hvs[hvs.length - 1]);
                                            HttpResponse resp = hc.execute(contentGet);
                                            if (resp.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                                                ByteArrayOutputStream bout = new ByteArrayOutputStream();
                                                resp.getEntity().writeTo(bout);
                                                String content = bout.toString().trim();
                                                bout.close();
                                                JSONObject json = new JSONObject(content);
                                                if (json != null) {
                                                    int ret = json.getInt("resultCode");
                                                    if (ret == ConstValueUtils.CODE_SUCCESS) {
                                                        String html = json.getString("content");
                                                        html = new String(decoder.decodeBuffer(html));
                                                        int startIndex = html.indexOf(startFlag);
                                                        startIndex = startIndex < 0 ? 0 : (startIndex + startFlag.length());
                                                        int endIndex = html.indexOf(endFlag);
                                                        endIndex = endIndex < 0 ? html.length() : endIndex;
                                                        String txt = html.substring(startIndex, endIndex);
                                                        BookDocPart bdp = new BookDocPart();
                                                        bdp.setTitle(title);
                                                        bdp.setSectionId(bds.getId());
                                                        bdp.setStoreType(ConstValueUtils.STORE_TYPE_DB);
                                                        bdp.setContent(txt);
                                                        bdp.setOrder(pOrder);
                                                        bdp.setUpdateTime(new Date());
                                                        daoSupport.saveEntity(bdp);
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        } catch (Exception ex) {
            return ConstValueUtils.CODE_EXCEPTION;
        }
        return ConstValueUtils.CODE_SUCCESS;
    }

    public int updateContent(BookDoc bd) {
        try {
            BaseDaoSupport daoSupport = SpringContextManager.getContext().getBean(BaseDaoSupport.class);
            JdbcTemplate jt = SpringContextManager.getContext().getBean(JdbcTemplate.class);
            List<BookDocSection> sectionList = daoSupport.listEntityByCondition(BookDocSection.class, " and bookId = ? order by order asc", new Object[]{bd.getId()});
            String maxPartSql = "select IFNULL(max(part_order),0) mo from book_document_part where section_id in ( select oid from book_document_section where book_id = ? )";
            int maxPartNum = jt.queryForInt(maxPartSql, new Object[]{bd.getId()});
            int currentPageNum = maxPartNum;
            Document doc = getDocument(URL_FIX + bd.getSourceId());
            if (doc != null) {
                Element titleEmt = doc.getElementById("identify");
                Node sNode = titleEmt.getNextSibling().getNextSibling();
                if (sNode.hasAttributes() && !sNode.getAttributes().getNamedItem("class").getNodeValue().equals("lian")) {
                    bd.setStatus(ConstValueUtils.STATUS_FINISHED);
                    daoSupport.saveEntity(bd);
                }
                BASE64Decoder decoder = new BASE64Decoder();
                String startFlag = "<div class=\"m-content\">";
                String endFlag = "<div id=\"book-bottom\">";
                NodeList emtList = doc.getElementsByTagName("DIV");
                int sNum = 0;
                int pOrder = 0;
                for (int i = 0; i < emtList.getLength(); i++) {
                    Node divNode = emtList.item(i);
                    if (divNode.hasAttributes()) {
                        NamedNodeMap attrMap = divNode.getAttributes();
                        Node clsAttrNode = attrMap.getNamedItem("class");
                        if (clsAttrNode != null) {
                            if (clsAttrNode.getNodeValue().contains("m-volume")) {
                                sNum++;
                                //处理章节
                                NodeList tList = ((Element) divNode).getElementsByTagName("H4");
                                String sectionName = "第" + sNum + "部分";
                                if (tList.getLength() > 0) {
                                    sectionName = tList.item(0).getTextContent().trim();
                                }
                                int cSectionId = 0;
                                if (sNum > sectionList.size()) {
                                    BookDocSection bds = new BookDocSection();
                                    bds.setBookId(bd.getId());
                                    bds.setName(sectionName);
                                    bds.setOrder(sNum);
                                    daoSupport.saveEntity(bds);
                                    cSectionId = bds.getId();
                                } else {
                                    BookDocSection bds = sectionList.get(sNum - 1);
                                    if (!bds.getName().equals(sectionName)) {
                                        bds.setName(sectionName);
                                        daoSupport.saveEntity(bds);
                                    }
                                    cSectionId = bds.getId();
                                }
                                NodeList ulList = ((Element) divNode).getElementsByTagName("UL");
                                if (ulList.getLength() > 0) {
                                    NodeList aList = ((Element) ulList.item(0)).getElementsByTagName("A");
                                    for (int a = 0; a < aList.getLength(); a++) {
                                        pOrder++;
                                        if (pOrder > maxPartNum) {
                                            Node aNode = aList.item(a);
                                            String title = aNode.getTextContent().trim();
                                            String pageHref = aNode.getAttributes().getNamedItem("href").getNodeValue().trim();
                                            String[] hvs = pageHref.split("/");
                                            if (hvs.length > 2) {
                                                HttpClient hc = new DefaultHttpClient();
                                                hc.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 60000);
                                                hc.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 30000);
                                                HttpGet contentGet = new HttpGet("http://yuedu.163.com/getArticleContent.do?sourceUuid=" + hvs[hvs.length - 2] + "&articleUuid=" + hvs[hvs.length - 1]);
                                                HttpResponse resp = hc.execute(contentGet);
                                                if (resp.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                                                    ByteArrayOutputStream bout = new ByteArrayOutputStream();
                                                    resp.getEntity().writeTo(bout);
                                                    String content = bout.toString().trim();
                                                    bout.close();
                                                    JSONObject json = new JSONObject(content);
                                                    if (json != null) {
                                                        int ret = json.getInt("resultCode");
                                                        if (ret == ConstValueUtils.CODE_SUCCESS) {
                                                            String html = json.getString("content");
                                                            html = new String(decoder.decodeBuffer(html));
                                                            int startIndex = html.indexOf(startFlag);
                                                            startIndex = startIndex < 0 ? 0 : (startIndex + startFlag.length());
                                                            int endIndex = html.indexOf(endFlag);
                                                            endIndex = endIndex < 0 ? html.length() : endIndex;
                                                            String txt = html.substring(startIndex, endIndex);
                                                            BookDocPart bdp = new BookDocPart();
                                                            bdp.setTitle(title);
                                                            bdp.setSectionId(cSectionId);
                                                            bdp.setStoreType(ConstValueUtils.STORE_TYPE_DB);
                                                            bdp.setContent(txt);
                                                            bdp.setOrder(pOrder);
                                                            bdp.setUpdateTime(new Date());
                                                            daoSupport.saveEntity(bdp);
                                                            currentPageNum = pOrder;
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                return currentPageNum - maxPartNum;
            }
        } catch (Exception ex) {
            return ConstValueUtils.CODE_SUCCESS;
        }
        return ConstValueUtils.CODE_SUCCESS;
    }

    private void dirDetail2(BookDoc bd) {
        try {
            BaseDaoSupport daoSupport = SpringContextManager.getContext().getBean(BaseDaoSupport.class);
            JdbcTemplate jt = SpringContextManager.getContext().getBean(JdbcTemplate.class);
            String checkTagSql = "select oid from book_document_tag where tag_name = ? ";
            Document doc = getDocument(URL_FIX + bd.getSourceId());
            if (doc != null) {
                Element checkEmtList = doc.getElementById("J_Contents");
                NodeList liList = checkEmtList.getElementsByTagName("LI");
                boolean flag = liList.getLength() > 0 ? true : false;
                if (flag) {
                    Element titleEmt = doc.getElementById("identify");
                    Node sNode = titleEmt.getNextSibling().getNextSibling();
                    if (sNode.hasAttributes() && sNode.getAttributes().getNamedItem("class").getNodeValue().equals("lian")) {
                        bd.setStatus(ConstValueUtils.STATUS_CONTINUE);
                    } else {
                        bd.setStatus(ConstValueUtils.STATUS_FINISHED);
                    }
                    Node pNode = titleEmt.getParentNode().getParentNode();
                    NodeList infoList = ((Element) pNode).getElementsByTagName("SPAN");
                    String keyWords = "";
                    int cIndex = 0;
                    for (int p = 0; p < infoList.getLength(); p++) {
                        Node item = infoList.item(p);
                        if (item.hasAttributes()) {
                            Node clsNode = item.getAttributes().getNamedItem("class");
                            if (clsNode != null && clsNode.getNodeValue().contains("bull")) {
                                cIndex++;
                                String txt = item.getTextContent().trim();
                                if (!StringUtils.isEmpty(txt)) {
                                    if (txt.startsWith("出版社：")) {
                                        bd.setPublisher(txt.substring(4));
                                    } else if (txt.endsWith("点击")) {
                                        bd.setViewCount(Integer.valueOf(txt.substring(0, txt.length() - 2)));
                                    } else if (txt.startsWith("关键词：")) {
                                        keyWords = txt.substring(4).trim();
                                    } else if (cIndex == 1) {
                                        NodeList partList = ((Element) item).getElementsByTagName("A");
                                        if (partList.getLength() > 0) {
                                            bd.setCateName(partList.item(0).getTextContent().trim());
                                        }
                                    }
                                }
                            }
                        }
                    }
                    NodeList emtList = doc.getElementsByTagName("DIV");
                    int bNum = 0;
                    for (int i = 0; i < emtList.getLength(); i++) {
                        Node divNode = emtList.item(i);
                        //m-introbox
                        if (divNode.hasAttributes()) {
                            NamedNodeMap attrMap = divNode.getAttributes();
                            Node clsAttrNode = attrMap.getNamedItem("class");
                            if (clsAttrNode != null) {
                                if (clsAttrNode.getNodeValue().contains("m-introbox")) {
                                    bNum++;
                                    if (bNum == 1) {
                                        NodeList pList = ((Element) divNode).getElementsByTagName("P");
                                        if (pList.getLength() > 0) {
                                            bd.setBookBrief(pList.item(0).getTextContent().trim());
                                        }
                                    }
//                                    else if (bNum == 2) {
//                                        NodeList pList = ((Element) divNode).getElementsByTagName("P");
//                                        if (pList.getLength() > 0) {
//                                            Node cNode = pList.item(pList.getLength() - 1);
//                                            bd.setAuthorBrief(cNode.getChildNodes().item(0).getTextContent().trim());
//                                        }
//                                    }
                                } else if (clsAttrNode.getNodeValue().contains("m-picbox")) {
                                    NodeList imgList = ((Element) divNode).getElementsByTagName("IMG");
                                    if (imgList.getLength() > 0) {
                                        String imgHref = imgList.item(0).getAttributes().getNamedItem("src").getNodeValue().trim();
                                        byte[] data = downHttpData(imgHref);
                                        if (data.length > 0) {
                                            bd.setHasIcon(true);
                                            bd.setIcon(BlobProxy.generateProxy(data));
                                        }
                                    }
                                }
                            }
                        }
                    }
                    bd.setUpdateTime(new Date());
                    daoSupport.saveEntity(bd);
                    if (keyWords.length() > 0) {
                        String[] keyArray = keyWords.split(",");
                        for (String key : keyArray) {
                            key = key.trim();
                            if (key.length() > 0) {
                                int tagId = 0;
                                List<Map<String, Object>> tagDbList = jt.queryForList(checkTagSql, new Object[]{key});
                                if (tagDbList.size() > 0) {
                                    tagId = ((Number) tagDbList.get(0).get("oid")).intValue();
                                } else {
                                    BookDocTag bt = new BookDocTag();
                                    bt.setName(key);
                                    daoSupport.saveEntity(bt);
                                    tagId = bt.getId();
                                }
                                BookDocTagRelation tr = new BookDocTagRelation();
                                tr.setBookId(bd.getId());
                                tr.setTagId(tagId);
                                daoSupport.saveEntity(tr);
                            }
                        }
                    }
                }
            }
        } catch (Exception ex) {
            logger.error("更新:" + bd.getName() + "异常", ex);
        }
    }

    private void dirDetail(BookDoc bd) {
        try {
            BaseDaoSupport daoSupport = SpringContextManager.getContext().getBean(BaseDaoSupport.class);
            JdbcTemplate jt = SpringContextManager.getContext().getBean(JdbcTemplate.class);
            String checkTagSql = "select oid from book_document_tag where tag_name = ? ";
            Document doc = getDocument(URL_FIX + bd.getSourceId());
            if (doc != null) {
                NodeList checkEmtList = doc.getElementsByTagName("DIV");
                boolean flag = false;
                for (int c = 0; c < checkEmtList.getLength(); c++) {
                    Node divNode = checkEmtList.item(c);
                    //m-volume
                    if (divNode.hasAttributes()) {
                        NamedNodeMap attrMap = divNode.getAttributes();
                        Node clsAttrNode = attrMap.getNamedItem("class");
                        if (clsAttrNode != null) {
                            if (clsAttrNode.getNodeValue().contains("m-volume")) {
                                flag = true;
                                break;
                            }
                        }
                    }
                }
                if (flag) {
                    Element titleEmt = doc.getElementById("identify");
                    Node pNode = titleEmt.getParentNode().getParentNode();
                    NodeList infoList = ((Element) pNode).getElementsByTagName("SPAN");
                    for (int p = 0; p < infoList.getLength(); p++) {
                        Node item = infoList.item(p);
                        String txt = item.getTextContent().trim();
                        if (!StringUtils.isEmpty(txt)) {
                            if (txt.endsWith("字")) {
                                bd.setWordNum(Integer.valueOf(txt.substring(0, txt.length() - 1)));
                            } else if (txt.startsWith("出版社：")) {
                                bd.setPublisher(txt.substring(4));
                            } else if (txt.endsWith("点击")) {
                                bd.setViewCount(Integer.valueOf(txt.substring(0, txt.length() - 2)));
                            }
                        }
                    }
                    NodeList emtList = doc.getElementsByTagName("DIV");
                    int bNum = 0;
                    for (int i = 0; i < emtList.getLength(); i++) {
                        Node divNode = emtList.item(i);
                        //m-introbox
                        if (divNode.hasAttributes()) {
                            NamedNodeMap attrMap = divNode.getAttributes();
                            Node clsAttrNode = attrMap.getNamedItem("class");
                            if (clsAttrNode != null) {
                                if (clsAttrNode.getNodeValue().contains("m-introbox")) {
                                    bNum++;
                                    if (bNum == 1) {
                                        NodeList pList = ((Element) divNode).getElementsByTagName("P");
                                        if (pList.getLength() > 0) {
                                            bd.setBookBrief(pList.item(0).getTextContent().trim());
                                        }
                                    } else if (bNum == 2) {
                                        NodeList pList = ((Element) divNode).getElementsByTagName("P");
                                        if (pList.getLength() > 0) {
                                            Node cNode = pList.item(pList.getLength() - 1);
                                            bd.setAuthorBrief(cNode.getChildNodes().item(0).getTextContent().trim());
                                        }
                                    }
                                }
                            }
                        }
                    }
                    bd.setCateName("出版图书");
                    bd.setUpdateTime(new Date());
                    daoSupport.saveEntity(bd);
                    int tagId = 0;
                    List<Map<String, Object>> tagDbList = jt.queryForList(checkTagSql, new Object[]{bd.getName()});
                    if (tagDbList.size() > 0) {
                        tagId = ((Number) tagDbList.get(0).get("oid")).intValue();
                    } else {
                        BookDocTag bt = new BookDocTag();
                        bt.setName(bd.getName());
                        daoSupport.saveEntity(bt);
                        tagId = bt.getId();
                    }
                    BookDocTagRelation tr = new BookDocTagRelation();
                    tr.setBookId(bd.getId());
                    tr.setTagId(tagId);
                    daoSupport.saveEntity(tr);
                    tagDbList = jt.queryForList(checkTagSql, new Object[]{bd.getAuthor()});
                    if (tagDbList.size() > 0) {
                        tagId = ((Number) tagDbList.get(0).get("oid")).intValue();
                    } else {
                        BookDocTag bt = new BookDocTag();
                        bt.setName(bd.getName());
                        daoSupport.saveEntity(bt);
                        tagId = bt.getId();
                    }
                    tr = new BookDocTagRelation();
                    tr.setBookId(bd.getId());
                    tr.setTagId(tagId);
                    daoSupport.saveEntity(tr);
                }
            }
        } catch (Exception ex) {
            logger.error("更新:" + bd.getName() + "异常", ex);
        }
    }

    private Document getDocument(String url) {
        try {
            HttpClient hc = new DefaultHttpClient();
            hc.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 60000);
            hc.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 30000);
            HttpGet hg = new HttpGet(url);
            HttpResponse response = hc.execute(hg);
            if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                DOMParser htc = new DOMParser();
                htc.setProperty("http://cyberneko.org/html/properties/default-encoding", CHARSET_HTML);
                htc.parse(new InputSource(response.getEntity().getContent()));
                hc.getConnectionManager().shutdown();
                return htc.getDocument();
            }
        } catch (Exception e) {
            logger.error("解析Html异常:" + url, e);
        }
        return null;
    }

    private byte[] downHttpData(String url) {
        try {
            HttpClient hc = new DefaultHttpClient();
            hc.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 60000);
            hc.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 30000);
            HttpGet hg = new HttpGet(url);
            HttpResponse response = hc.execute(hg);
            if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                ByteArrayOutputStream bout = new ByteArrayOutputStream();
                response.getEntity().writeTo(bout);
                return bout.toByteArray();
            }
        } catch (Exception e) {
            logger.error("解析Html异常:" + url, e);
        }
        return null;
    }
}
